package com.example.ai.app;

import com.example.ai.advisor.MyLoggerAdvisor;
import com.example.ai.chatmemory.FileBasedChatMemory;
import com.example.ai.rag.HappyAppRagCustomAdvisorFactory;
import com.example.ai.rag.QueryRewriter;
import com.example.ai.tool.ResourceDowmloadTool;
import com.example.ai.tool.ToolRagistration;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;

import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.tool.ToolCallbackProvider;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;

import java.util.List;

import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;
import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_RETRIEVE_SIZE_KEY;

@Slf4j
@Component
public class HappyApp {
    private static final String SYSTEM_PROMPT = "你好";
    private final ChatClient chatClient;

    @Resource
    private VectorStore happyAppVectorStore;

    @Resource
    private ToolRagistration allTools;
    @Autowired
    private QueryRewriter queryRewriter;
    //  @Resource
   // private VectorStore pgVectorVectorStore ;

    public HappyApp(ChatModel dashscopeChatModel) {
        String filedir = System.getProperty("user.dir")+"/tmp/chat_memory";
        ChatMemory chatMemory = new FileBasedChatMemory(filedir);
       //ChatMemory chatMemory = new InMemoryChatMemory();
       chatClient = ChatClient.builder(dashscopeChatModel)
                .defaultSystem(SYSTEM_PROMPT)
                .defaultAdvisors(
                        new MessageChatMemoryAdvisor(chatMemory)
                       // new SimpleLoggerAdvisor()
                )
               .build();


    }

    public String chat(String message, String chatId) {
        ChatResponse response = chatClient.prompt()
                .user(message)
                .advisors(spec -> spec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId)
                        .param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 10))
                .call()
                .chatResponse();
        String content = null;
        if(response != null){

        content = response.getResult().getOutput().getText();
        }
        log.info("content: {}", content);
        return content;
    }




    record ActorsFilms(String actor, List<String> movies) {

    }
    public ActorsFilms chatReport(String message, String chatId) {
        ActorsFilms actorsFilms = chatClient.prompt()
                .system(SYSTEM_PROMPT)
                .user(message)
                .advisors(spec -> spec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId)
                    .param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 10))
                .call()
                .entity(ActorsFilms.class);
        log.info("ActorsFilms: {}", actorsFilms);
        return actorsFilms;
    }

    //本地文件
    public String chatWithRag(String message,String chatId) {
        ChatResponse response = chatClient.prompt()
                .user(message)
                .advisors(spec -> spec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId)
                        .param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 10))
                .advisors(new MyLoggerAdvisor())
                .advisors(new QuestionAnswerAdvisor(happyAppVectorStore))
                .advisors(HappyAppRagCustomAdvisorFactory.createAppCustomerAdvisor(happyAppVectorStore,"介绍游戏王"))
                .call()
                .chatResponse();
        String content = null;
        if (response != null) {
            content = response.getResult().getOutput().getText();
        }
        log.info("context:{}",content);
        return content;
    }



    public String chatWithCloudRag(String message,String chatId) {
        ChatResponse response = chatClient.prompt()
                .user(message)
                .advisors(spec -> spec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId)
                        .param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 10))
                .advisors(new MyLoggerAdvisor())
                //.advisors(happyAppRagCloudAdvisor)
                .call()
                .chatResponse();
        String content = null;
        if (response != null) {
            content = response.getResult().getOutput().getText();
        }
        log.info("context:{}",content);
        return content;
    }

    public String chatWithVectorRag(String message,String chatId) {
        ChatResponse response = chatClient.prompt()
                .user(message)
                .advisors(spec -> spec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId)
                        .param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 10))
                .advisors(new MyLoggerAdvisor())
                //.advisors(new QuestionAnswerAdvisor(pgVectorVectorStore))
                .call()
                .chatResponse();
        String content = null;
        if (response != null) {
            content = response.getResult().getOutput().getText();
        }
        log.info("context:{}",content);
        return content;
    }
    public String dochatWithtool(String message,String chatId) {
        ChatResponse response = chatClient.prompt()
                .user(message)
                .advisors(spec -> spec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId)
                        .param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 10))
                .advisors(new MyLoggerAdvisor())
                .tools(allTools)
                .call()
                .chatResponse();
        String content = null;
        if (response != null) {
            content = response.getResult().getOutput().getText();
        }
        log.info("context:{}",content);
        return content;
    }
    @Resource
    private ToolCallbackProvider toolCallbackProvider;

    public String dochatWithmcp(String message,String chatId) {
        ChatResponse response = chatClient.prompt()
                .user(message)
                .advisors(spec -> spec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId)
                        .param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 10))
                .advisors(new MyLoggerAdvisor())
                .tools(toolCallbackProvider)
                .call()
                .chatResponse();
        String content = null;
        if (response != null) {
            content = response.getResult().getOutput().getText();
        }
        log.info("context:{}",content);
        return content;
    }
    public Flux<String> doChatBySteam(String message,String chatId) {
        message = queryRewriter.doQueryRewrite(message);
        return chatClient.prompt()
                .user(message)
                .advisors(spec -> spec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId)
                        .param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 10))
                .stream()
                .content();
    }



}
